/* 
 * Copyright (C) 2008-2010 Josef Hardi <josef.hardi@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.bth.student.android.photohand.parser;

import java.util.Hashtable;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import edu.bth.student.android.photohand.provider.PhotoHandSchema.Albums;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;

public class PicasaWebAlbumHandler extends DefaultHandler {

  private static final String ATOM_NAMESPACE = "http://www.w3.org/2005/Atom";
  private static final String GPHOTO_NAMESPACE = 
      "http://schemas.google.com/photos/2007";

  private boolean albumNameTag = false;
  private boolean albumTitleTag = false;
  private boolean albumOwnerTag = false;
  private boolean albumUpdatedTag = false;
  private boolean entryTag = false;

  private ContentResolver content;
  private ContentValues values;

  private StringBuilder builder;

  private String albumName;
  private Hashtable<String, Uri> albumUris = new Hashtable<String, Uri>();

  public PicasaWebAlbumHandler(ContentResolver content) {
    this.content = content;
    this.values = new ContentValues();
  }

  public Hashtable<String, Uri> getAlbumList() {
    return albumUris;
  }

  @Override
  public void startDocument() throws SAXException {
    // Do nothing
  }

  @Override
  public void endDocument() throws SAXException {
    // Do nothing
  }

  @Override
  public void startElement(String uri, String localName, String qName,
      Attributes attributes) throws SAXException {
    if (uri.equals(ATOM_NAMESPACE)) {
      if (localName.equals("entry")) {
        entryTag = true;
        values.clear();
      }

      if (entryTag) { // If it is inside the <entry> tag.
        if (localName.equals("updated")) {
          albumUpdatedTag = true;
          builder = new StringBuilder();
        } else if (localName.equals("title")) {
          albumTitleTag = true;
          builder = new StringBuilder();
        }
      }
    }

    if (uri.equals(GPHOTO_NAMESPACE)) {
      if (entryTag) { // If it is inside the <entry> tag.
        if (localName.equals("name")) {
          albumNameTag = true;
          builder = new StringBuilder();
        } else if (localName.equals("nickname")) {
          albumOwnerTag = true;
          builder = new StringBuilder();
        }
      }
    }
  }

  @Override
  public void endElement(String uri, String localName, String qName)
      throws SAXException {
    if (uri.equals(ATOM_NAMESPACE)) {
      if (localName.equals("entry")) {
        entryTag = false;
        Uri albumUri = content.insert(Albums.CONTENT_URI, values);
        albumUris.put(albumName, albumUri);
      }

      if (entryTag) { // If it is inside the <entry> tag.
        if (localName.equals("updated")) {
          albumUpdatedTag = false;
          values.put(Albums.UPDATED_ON, getText());
        } else if (localName.equals("title")) {
          albumTitleTag = false;
          values.put(Albums.TITLE, getText());
        }
      }
    }

    if (uri.equals(GPHOTO_NAMESPACE)) {
      if (entryTag) { // If it is inside the <entry> tag.
        if (localName.equals("name")) {
          albumNameTag = false;
          albumName = getText();
        } else if (localName.equals("nickname")) {
          albumOwnerTag = false;
          values.put(Albums.OWNER, getText());
        }
      }
    }
  }

  @Override
  public void characters(char text[], int start, int length) {
    if (builder != null) {
      builder.append(text, start, length);
    }
  }

  private String getText() {
    try {
      return builder.toString();
    } finally {
      builder = null;
    }
  }
}
